// Copyright 2011 Bryan McKelvey, MIT License

/**
 * @fileoverview 
 * @author bryan.mckelvey (McKelvey Bryan)
 */

/**
 * This is an object representing methods available on the options page
 * @constructor
 */
var Options = (function(){
	/**
	 * The background page
	 * @type function
	 */
	var bg = chrome.extension.getBackgroundPage();
	var portEdit = document.getElementById("portfolioEdit");
	var metricsEdit = document.getElementById("metricsEdit");
	var updateFreq = document.getElementById("updateFrequency");
	var syncBox = document.getElementById("syncBox");
	var injectBox = document.getElementById("injectBox");
	
	/*
	function syncLocalStorage() {
	  sync();
	}
	*/
	
	return {
		/**
		 * Reset securities to their defaults
		 * @returns The current object
		 * @type Options
		 */
		resetSecs: function(){
			var answer = confirm(chrome.i18n.getMessage("resetConfirm"));
			if (answer) {
				bg.Investor.reset();
				bg.Metrics.reset();
				window.location.reload();
			}
			return this;
		},
		
		/**
		 * Undoes any changes made in the current window by reloading the page
		 */
		undo: function(){
			window.location.reload();
		},
		
		/**
		 * Clears all securities and resets the metrics, then reloads the page
		 */
		clearAll: function(){
			var answer = confirm(chrome.i18n.getMessage("clearConfirm"));
			if (answer) {
				bg.Investor.clear();
				bg.Metrics.reset();
				window.location.reload();
			}
		},
		
		/**
		 * Saves all of the options on the page
		 * @returns The current object
		 * @type Options
		 */
		save: function(){
			try {
				bg.Investor.current().updateQuotes(portEdit);
				bg.Investor.loadTickers();
				bg.Investor.save();
				// bg.Metrics.save();
				Settings.language($("#language").val());
				localStorage["sync"] = (syncBox.value === "on");
				localStorage["inject"] = (injectBox.value === "on");
				localStorage["updateFrequency"] = updateFreq.value;
				bg.Investor.frequency = updateFreq.value * 1000;
				writeBookmark(function(){
					alert("Saved!");
				});
			} 
			catch (e) {
				alert("Error saving!");
			}
			return this;
		},
		
		/**
		 * Renames the current portfolio and reloads the page
		 */
		renamePortfolio: function(){
			var name = prompt("Rename your portfolio", bg.Investor.current().name);
			if (name != null) {
				bg.Investor.current().name = name;
			}
			window.location.reload();
		},
		
		/**
		 * Deletes the current portfolio and reloads the page
		 */
		deletePortfolio: function(){
			if (bg.Investor.count() > 1) {
				var curIndex = bg.portfolios.currentIndex;
				bg.portfolios.portfolios.splice(curIndex, 1);
				if (bg.portfolios.portfolios.length > curIndex + 1) {
					bg.portfolios.currentIndex = curIndex - 1;
				}
			}
			window.location.reload();
		},
		
		/**
		 * Loads the page, including localization and displaying all the options
		 * currently saved for the user
		 * @returns The current object
		 * @type Options
		 */
		load: function(){
			// Main processes
			bg.Investor.createDropDown($("#portfolioSelect")[0], portEdit, true);
			bg.Investor.current().editTable(portEdit);
			bg.Metrics.dropdowns($(metricsEdit));
			
			// Select option
			for (var i = 0; i < updateFreq.options.length; i++) {
				var option = updateFreq.options[i];
				
				if (parseInt(option.value) * 1000 === Settings.frequency()) {
					updateFreq.value = option.value;
				}
			}
			
			$("#language").val(Settings.language());
			
			if (localStorage.sync == "true") {
				syncBox.checked = "checked";
			}
			
			if (localStorage.inject == "true") {
				injectBox.checked = "checked";
			}
			
			bg.Localizer.localize($(document));
			var tableDnD = new TableDnD();
			tableDnD.init(portEdit.getElementsByTagName("table")[0]);
			return this;
		}
	}
})();

Options.load();